package view.diaLogView;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.AppCompatCheckBox;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.administrator.text.R;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import util.LogUtil;
import util.payUtil.PayUtil;

/**
 * Created by Administrator on 2016/6/21.
 *选择支付的 dialog
 */
public class ChoicePaymentDialog extends Dialog {
    private String stringData,out_trade_no,subject,body,cooperationid,datatime;
    private double total_fee;
    private final Activity mContext;
    private ChoicePaymentDialog choicePaymentDialog;

    public ChoicePaymentDialog(Activity context,String string) {
        super(context, R.style.upoad_dialog);
        this.mContext = context;
        this.stringData = string;
        choicePaymentDialog = this;
    }

    @Override public void show() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.share_style);
        super.show();
    }

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.width = mContext.getResources().getDisplayMetrics().widthPixels;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = inflater.inflate(R.layout.choicepayment_dialog, null);
        TextView TextViewOK = (TextView) mView.findViewById(R.id.tv_dialog_choicepayment);
        final AppCompatCheckBox appCompatCheckBoxPay = (AppCompatCheckBox) mView.findViewById(R.id.checkbox_pay);
        final AppCompatCheckBox appCompatCheckBoxWechat = (AppCompatCheckBox) mView.findViewById(R.id.checkbox_wechat);
        LinearLayout linearLayout1 = (LinearLayout) mView.findViewById(R.id.dialog_alipay);  //支付宝
        LinearLayout linearLayout2 = (LinearLayout) mView.findViewById(R.id.dialog_wechat); //微信

        linearLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.eE("","setOnClickListener..........支付宝");
                appCompatCheckBoxPay.setClickable(true);
                appCompatCheckBoxWechat.setChecked(false);
                PayUtil.NewPayUtil().getFEQAlipay(mContext,subject,body,total_fee,out_trade_no,cooperationid,datatime);
                choicePaymentDialog.dismiss();
            }
        });

        linearLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.eE("","setOnClickListener..........微信");
                appCompatCheckBoxPay.setClickable(true);
                appCompatCheckBoxWechat.setChecked(false);
                PayUtil.NewPayUtil().getFEQWeChatPay(mContext,subject,body,total_fee,out_trade_no,cooperationid,datatime);
                choicePaymentDialog.dismiss();
            }
        });

        appCompatCheckBoxPay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    LogUtil.eE("","onCheckedChanged..........支付宝");
                    appCompatCheckBoxWechat.setChecked(false);
                    appCompatCheckBoxPay.setClickable(true);
                    PayUtil.NewPayUtil().getFEQAlipay(mContext,subject,body,total_fee,out_trade_no,cooperationid,datatime);
                    choicePaymentDialog.dismiss();
                }
            }
        });

        appCompatCheckBoxWechat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    LogUtil.eE("","onCheckedChanged..........微信");
                    appCompatCheckBoxPay.setChecked(false);
                    appCompatCheckBoxWechat.setChecked(true);
                    PayUtil.NewPayUtil().getFEQWeChatPay(mContext,subject,body,total_fee,out_trade_no,cooperationid,datatime);
                    choicePaymentDialog.dismiss();
                }
            }
        });

        TextViewOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choicePaymentDialog.dismiss();
            }
        });

        try {
            JSONObject jsonObject = new JSONObject(stringData);
            if(jsonObject.has("out_trade_no")){
                out_trade_no = jsonObject.getString("out_trade_no");    //订单ID
            }
            if(jsonObject.has("subject")){
                subject = jsonObject.getString("subject");            //商品,名称
            }
           if(jsonObject.has("body")){
               body = jsonObject.getString("body");                 //交易说明
            }
            if(jsonObject.has("total_fee")){
                total_fee = jsonObject.getDouble("total_fee");    //价格
            }
            if(jsonObject.has("cooperationid")){
                cooperationid = jsonObject.getString("cooperationid"); //合作id
            }

            if(jsonObject.has("datatime")){
                datatime = jsonObject.getString("datatime");  //时间
            }

            LogUtil.eE("","....datatime..."+datatime+"...cooperationid..."+cooperationid+"...total_fee..."+total_fee);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        setContentView(mView, params);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            EventBus.getDefault().post("state");
            choicePaymentDialog.dismiss();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
